// 1. express 框架
const express = require("express");
const fs = require("fs");
const path = require("path");

// 2.创建一个后台路由
const router = express.Router();

router.get("/navlist", async (req, res) => {
    let navsPath = path.join(__dirname, "../../public/data/navs.json");

    fs.readFile(navsPath, "utf-8", (err, text) => {
        // console.log("err:",err);
        // console.log("text:",text);
        if (err) {
            res.send({
                code: -1,
                err,
            });
        } else {
            let navsData = JSON.parse(text);
            // 响应给前端
            res.send({
                code: 200,
                result: navsData,
            });
        }
    });
});

router.get("/lists", async (req, res) => {
    let listsPath = path.join(__dirname, "../../public/data/lists.json");

    fs.readFile(listsPath, "utf-8", (err, text) => {
        if (err) {
            res.send({
                code: -1,
                err,
            });
        } else {
            let listsData = JSON.parse(text);
            res.send({
                code: 200,
                result: listsData,
            });
        }
    });
});

//把当前路由暴露在模块系统下
module.exports = router;
